/**
 * Copyright 2011 Poltman.Ltd; www.poltman.com. All rights reserved.		
 *
 * This file is part of the dspace-poltman project http://www.poltman.com.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of DuraSpace DSpace license detailed in the LICENSE and NOTICE 
 * files at the root of the source tree and available online at
 * http://www.dspace.org/license/
 * 
 */

package com.poltman.dscentral.extended.services.impl;


import org.apache.log4j.Logger;
import org.dspace.handle.HandleManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.poltman.dscentral.extended.services.CommunityExtendedService;
import com.poltman.dscentral.db.entity.dspace.CommunityEntity;
import com.poltman.dscentral.db.entity.dspace.EpersongroupEntity;
import com.poltman.dscentral.db.entity.dspace.ResourcepolicyEntity;
import com.poltman.dscentral.db.exceptions.RepositoryAccessException;
import com.poltman.dscentral.db.exceptions.RepositoryException;
import com.poltman.dscentral.db.model.zul.CommunityEditionZulModel;
import com.poltman.dscentral.db.service.dspace.CommunityService;
import com.poltman.dscentral.db.service.dspace.EpersongroupService;
import com.poltman.dscentral.db.service.dspace.ResourcepolicyService;


/**
 * 
 * @author z.ciok@poltman.com
 *
 */
@Service("communityExtendedService")
public class CommunityExtendedServiceImpl implements CommunityExtendedService {
	
	private static Logger log = Logger.getLogger(CommunityExtendedServiceImpl.class);
	
	@Autowired
	private CommunityService communityService;
	
	@Autowired
	private EpersongroupService epersongroupService;
	
	@Autowired
	private ResourcepolicyService resourcepolicyService;		
	
	@Transactional(rollbackFor = {RepositoryException.class, RepositoryAccessException.class})
	@Override
	public CommunityEditionZulModel createTopLevelCommunity(CommunityEditionZulModel zulModel) throws DataAccessException, RepositoryException {
		CommunityEditionZulModel aZulModel = new CommunityEditionZulModel();
		try {
			//create community record
			CommunityEntity community = new CommunityEntity();
			community.setName(zulModel.getName());
			community.setShortDescription(zulModel.getShortDescription());
			community = communityService.save(community);
			
			//create handle
			String handle = HandleManager.createHandle(org.dspace.core.Constants.COMMUNITY, community.getCommunityId().intValue());
			
	        // create the default authorization policy for communities
	        // of 'anonymous' READ
	        //Group anonymousGroup = Group.find(context, 0);
	        EpersongroupEntity epersongroup = epersongroupService.findByEpersonGroupId(org.dspace.core.Constants.EPERSON_ANONYMOUS_GROUP);
	        if (epersongroup == null) {
				log.error("CommunityExtendedServiceImpl.createTopLevelCommunity Error; anonymous group doesn't exist.");
				throw new RepositoryException("CommunityExtendedServiceImpl.createTopLevelCommunity Error; anonymous group doesn't exist.");
	        }
	        
	        ResourcepolicyEntity resourcepolicyEntity = new ResourcepolicyEntity();
	        resourcepolicyEntity.setActionId(org.dspace.core.Constants.READ);
	        resourcepolicyEntity.setStartDate(null);
	        resourcepolicyEntity.setEndDate(null);
	        resourcepolicyEntity.setEpersongroupId(org.dspace.core.Constants.EPERSON_ANONYMOUS_GROUP);
	        resourcepolicyEntity.setEpersonId(null);
	        resourcepolicyEntity.setResourceTypeId(org.dspace.core.Constants.COMMUNITY);
	        resourcepolicyEntity.setResourceId(community.getCommunityId());
	        resourcepolicyService.save(resourcepolicyEntity);
			
	        //returned object
			aZulModel.setName(community.getName());
			aZulModel.setShortDescription(community.getShortDescription());
			aZulModel.setHandle(handle);
		} catch (RepositoryAccessException dae) {
			log.error("CommunityExtendedServiceImpl.createTopLevelCommunity Error; ", dae);
			throw dae;
		} catch (RepositoryException e) {
			log.error("CommunityExtendedServiceImpl.createTopLevelCommunity Error; ", e);
			throw e;
		}
		
		return aZulModel;
	}
}